package com.fjzx.androidframe.base.fragment;

import android.arch.lifecycle.ViewModel;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.fjzx.androidframe.base.R;
import com.fjzx.androidframe.base.adapter.BasePagedRecyclerViewAdapter;
import com.fjzx.androidframe.base.callback.OnItemClickListener;
import com.fjzx.androidframe.base.widget.xrecyclerview.XRecyclerView;

/**
 * @author xiongyongcong
 */
public abstract class BaseListFragment<VM extends ViewModel> extends BaseFragment<VM> implements OnItemClickListener {
    protected FloatingActionButton fabBackTop;
    protected FrameLayout flFilterContainer;
    protected DrawerLayout dlFilter;
    protected XRecyclerView rvList;

    protected LinearLayoutManager layoutManager;
    protected BasePagedRecyclerViewAdapter mAdapter;

    @Override
    protected View bindContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.base_page_xrecyclerview, container, false);
    }

    @Override
    protected void initView(View view) {
        mAdapter = bindAdapter();
        mAdapter.setOnItemClickListener(this);
        layoutManager = new LinearLayoutManager(mActivity);
        rvList = view.findViewById(R.id.rv_list);
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(mAdapter);
        rvList.setOnPullLoadMoreListener(new XRecyclerView.SimplePullLoadMoreListener() {
            @Override
            public void onRefresh() {
                mAdapter.refresh();
            }
        });

        fabBackTop = view.findViewById(R.id.fab_backtop);
        fabBackTop.setOnClickListener(v -> backTop());
        fabBackTop.setVisibility(backTopEnable() ? View.VISIBLE : View.GONE);

        dlFilter = view.findViewById(R.id.dl_filter);
        dlFilter.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        flFilterContainer = view.findViewById(R.id.fl_filter_container);
        View filterView = bindDrawerFilterView();
        ViewGroup.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        if (filterView != null) {
            flFilterContainer.addView(filterView, params);
        }
    }

    protected boolean backTopEnable() {
        return true;
    }

    protected void backTop() {
        // 先scrollTo 一屏最后一个位置，然后再smoothTo第一个位置
        int startPosition = layoutManager.findFirstVisibleItemPosition();
        int endPosition = layoutManager.findLastVisibleItemPosition();
        int sizeVisible = endPosition - startPosition;
        if (startPosition > sizeVisible) {
            rvList.getRecyclerView().scrollToPosition(sizeVisible);
        }
        rvList.getRecyclerView().smoothScrollToPosition(0);
    }

    public void toogleFilterDrawer() {
        boolean drawerOpen = dlFilter.isDrawerOpen(Gravity.END);
        if (drawerOpen) {
            dlFilter.closeDrawer(Gravity.END);
        } else {
            dlFilter.openDrawer(Gravity.END);
        }
    }

    @Override
    protected void handleLoading(String action, int msgId) {
        rvList.setRefreshing(true);
    }

    @Override
    protected void handleError(String action, String message) {
        if (mAdapter.getCurrentList() == null || mAdapter.getCurrentList().isEmpty()) {
            rvList.showError(message);
        } else {
            super.handleError(action, message);
        }
    }

    @Override
    protected <T> void handleSuccess(String action, T data) {
        super.handleSuccess(action, data);
        rvList.setRefreshing(false);
        if (data == null) {
            if (mAdapter.getCurrentList() == null || mAdapter.getCurrentList().isEmpty()) {
                rvList.showEmpty();
            } else {
                rvList.setHasMore(false);
            }
        }
    }

    protected abstract BasePagedRecyclerViewAdapter bindAdapter();

    protected View bindDrawerFilterView() {
        return null;
    }
}
